############################################
###### Replication Code for Figure A1 ######
########### January 9th, 2025 ##############
############################################

### Script produces Figure A1 and saves it in the `/figures` folder

rm(list=ls())

# load libraries
library(dplyr)
library(fixest)
library(ggplot2)
library(stringr)
library(MatchIt)
library(ebal)

# load main data
df <- readRDS("data/workfile.rds")

# generate empty dataframe for coefs
coef_df <- data.frame()

##### Outparty Feeling
m1 <- feols(mean_outparty_feeling ~ treated_econ + treated_culture, df, se = "iid") %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                      se = m1$se[2:3],
                      name = c("Economic Dimension", "Cultural Dimension"),
                      var =  "Average Outparty \n Feeling")
coef_df <- rbind.data.frame(coef_df, temp)

##### Outparty Social Distance
m1 <- feols(mean_close_num ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "Average Outparty \n Social Distance")
coef_df <- rbind.data.frame(coef_df, temp)


##### Gender
m1 <- feols(dem_female ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "Female")
coef_df <- rbind.data.frame(coef_df, temp)

##### Rural urban
m1 <- feols(dem_rural ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "Rural")
coef_df <- rbind.data.frame(coef_df, temp)

##### Age
df$age_std <- scale(df$dem_age)
m1 <- feols(age_std ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "Age standardized")
coef_df <- rbind.data.frame(coef_df, temp)


##### High edu
m1 <- feols(high_edu ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "High education")
coef_df <- rbind.data.frame(coef_df, temp)

###### High income
m1 <- feols(high_income ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "High income")
coef_df <- rbind.data.frame(coef_df, temp)

###### Inparty Feeling
m1 <- feols(in_party_feeling ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "Inparty Feeling")
coef_df <- rbind.data.frame(coef_df, temp)

feols(in_party_feeling ~ treated_econ + control, df) %>% 
  summary()

###### We vs they
m1 <- feols(we_vs_they_party ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "Use of we vs. they")
coef_df <- rbind.data.frame(coef_df, temp)

###### Left-right scale
m1 <- feols(lrscale ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "Left-right scale")
coef_df <- rbind.data.frame(coef_df, temp)

###### High political interest
m1 <- feols(high_pol_interest ~ treated_econ + treated_culture, df) %>% 
  summary()
temp <- data.frame(coef = m1$coefficients[2:3], 
                   se = m1$se[2:3],
                   name = c("Economic Dimension", "Cultural Dimension"),
                   var =  "High political \n interest")
coef_df <- rbind.data.frame(coef_df, temp)


pd <- position_dodge(0.5)

regs <- ggplot(coef_df, aes(x = var,y = coef, color = name)) + 
  #ylim(c(-0.15, 0.05)) +
  geom_point(position = pd, shape = 21, fill = 'white') + 
  geom_errorbar(aes( ymin = coef - se*1.959964, ymax = coef + se*1.959964), width = 0, position = pd) + 
  geom_errorbar(aes( ymin = coef - se*1.644854, ymax = coef + se*1.644854), width = 0, position = pd, size = 1.5) + 
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  scale_colour_manual(values = c("black", "grey")) + 
  xlab("") + 
  ylab("Treatment effect") +
  coord_flip() +
  #facet_wrap(~ var) + 
  theme_bw() + 
  guides(color=guide_legend(title=""))
regs

ggsave("figures/FigA1_balance_coef.pdf",
       height = 6, width = 8)

